use std::path::PathBuf;

fn get_audio_path(relative_path: &str) -> Result<PathBuf, Box<dyn std::error::Error>> {
    // 尝试多个可能的路径
    let possible_paths = vec![
        // 开发环境路径
        PathBuf::from(relative_path),
        // 项目根目录路径
        PathBuf::from(format!("../{}", relative_path)),
        // 当前工作目录
        std::env::current_dir()?.join(relative_path),
        // 可执行文件目录
        std::env::current_exe()?.parent().ok_or("No parent dir")?.join(relative_path),
        // 资源目录（打包后）
        std::env::current_exe()?.parent().ok_or("No parent dir")?.join("resources").join(relative_path.trim_start_matches("public/")),
    ];
    
    println!("Searching for audio file: {}", relative_path);
    for (i, path) in possible_paths.iter().enumerate() {
        println!("  [{}] Trying: {:?} - exists: {}", i, path, path.exists());
        if path.exists() {
            println!("  ✓ Found at: {:?}", path);
            return Ok(path.clone());
        }
    }
    
    Err(format!("Audio file not found in any location: {}", relative_path).into())
}

fn main() {
    println!("Current working directory: {:?}", std::env::current_dir().unwrap());
    println!("Current executable: {:?}", std::env::current_exe().unwrap());
    println!();
    
    let test_files = vec![
        "public/video/placeOrder.wav",
        "public/video/sucess.wav",
        "public/video/longOnce.wav",
        "public/video/fiveTimes.wav",
        "public/video/cancelOrder.wav",
    ];
    
    for file in test_files {
        match get_audio_path(file) {
            Ok(path) => println!("✓ {} -> {:?}", file, path),
            Err(e) => println!("✗ {} -> {}", file, e),
        }
        println!();
    }
}
